<template>
  <div id="app">
    <el-container>
      <el-aside width="200px">
        <h2>导航</h2>
        <el-menu
            class="el-menu-vertical-demo"
            @select="select"
        >
          <el-submenu v-for="(group, index) in navigation" :key="index" :index="index.toString()">
            <template slot="title">
              <i class="el-icon-notebook-2"></i>
              <span>{{ group.title }}</span>
            </template>
            <el-menu-item v-for="(menu, i) in group.menu" :key="i" :index="index.toString() + ',' + i.toString()">
              {{ menu.title }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 对应的组件内容渲染到router-view中 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>

export default {
  name: 'App',
  data: function () {
    return {
      index: "",
      navigation: [
        {
          title: "入门",
          menu: [
            {
              route: "/hello",
              title: "hello",
            },
          ],
        },
      ]
    }
  },
  created() {
    for (let i = 0; i < this.navigation.length; i++) {
      for (let j = 0; j < this.navigation[i].menu.length; j++) {
        if (this.navigation[i].menu[j].route === this.$route.path)
          this.index = `${i},${j}`
      }
    }
    if (this.index.length === 0 && this.$route.path !== "/")
      window.location.href = "/"
  },
  methods: {
    select: function (index) {
      if (index === this.index)
        return
      let is = index.split(",")
      let i1 = parseInt(is[0])
      let i2 = parseInt(is[1])
      this.$router.push(this.navigation[i1].menu[i2].route)
      this.index = index
    },
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
